package camera;

import UserInterface.FinalGUI;
import static com.googlecode.javacv.cpp.opencv_core.cvScalar;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_MEDIAN;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import coordinates.MeanDetector;
import java.util.logging.Level;
import java.util.logging.Logger;
import objectExtraction.ColourDetector;
import utilities.AngleFinder;
import com.googlecode.javacv.FrameGrabber.Exception;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class ColorMeanCamera implements Runnable {

    final int INTERVAL = 10;// 1sec
    final int CAMERA_NUM; // Default camera for this time
    final int CAMERA_FOV;//angle ofthe camera in degrees
    private int IMAGE_NO;
    static CvScalar rgba_min = cvScalar(0, 0, 130, 0);// RED wide dabur birko
    static CvScalar rgba_max = cvScalar(80, 80, 255, 0);
    ColourDetector colDet = new ColourDetector();
    AngleFinder angFind = new AngleFinder();
    MeanDetector meanDet = new MeanDetector();
    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam Live");
    CanvasFrame path = new CanvasFrame("Threshold");
    private double theta;

    public ColorMeanCamera() {
        CAMERA_NUM = 0;
        CAMERA_FOV = 90;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        path.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        //path.setContentPane(jp);
    }

    public ColorMeanCamera(int camera, int fov) {
        CAMERA_NUM = camera;
        CAMERA_FOV = fov;
        canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        path.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        // path.setContentPane(jp);
    }

    @Override
    public void run() {
        IMAGE_NO = FinalGUI.initializeCamera();
        FrameGrabber grabber = new VideoInputFrameGrabber(CAMERA_NUM);
        int posX;
        try {
            grabber.start();
            Thread.sleep(1000);
            IplImage img, imTemp;
            IplImage detectThrs, detT;
            while (true) {
                img = grabber.grab();
                if (img != null) {
                    imTemp = img.clone();
                    // show image on window
                    //cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise

                    //System.out.println("wwwwwwwwwwwwwwwwww");
                    detectThrs = colDet.getThresholdImage1(img);
                    detT = detectThrs.clone();
                    //System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
                 //   cvSmooth(detectThrs, detectThrs, CV_MEDIAN, 15);
                    //cvSaveImage(++ii + "GreenThreshold.jpg", imgThreshold);
                    //System.out.println("hghsgsgsg");
                    //detectThrs = this.getThresholdImage1(img);
                    posX = meanDet.getMeanX(detectThrs);

                    CvPoint start = new CvPoint(posX, 0);
                    CvPoint end = new CvPoint(posX, detectThrs.width());
                    if (posX > 0) {
                        cvDrawLine(imTemp, start, end, CvScalar.RED, 4, 8, 0);
                    }
                    //cvDrawCircle(imTemp, fingerTip, 15, CvScalar.RED, -1, 8, 0);
                    theta = angFind.angleFinder(posX, detectThrs.width(), CAMERA_FOV);
                    path.showImage(detectThrs);
                    FinalGUI.setCamera( imTemp,detT, IMAGE_NO);
                    //System.out.println("theeeee");
                    canvas.showImage(img);
                }
                //Thread.sleep(INTERVAL);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ColorMeanCamera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
            System.out.println("Exception in Color Mean Detector");
            e.printStackTrace();
        }
    }

    /**
     * @return the theta
     */
    public double getTheta() {
        return theta;
    }
}
